package com.cobra.rdd.operator.transform

import org.apache.spark.{SparkConf, SparkContext}

//connect+group
object Spark28_RDD_Operator_Transform_cogroup {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Spark28_RDD_Operator_Transform_cogroup")
    val sc = new SparkContext(sparkConf)
    val rdd1 = sc.makeRDD(List(
      ("a", 1),
      ("b", 2),
      ("c", 3)
    ))
    val rdd2 = sc.makeRDD(List(
      ("a", 4),
      ("b", 5),
      ("c", 6),
      ("a",1)
    ))
    //如果数据源中的key没有匹配上。那么数据不会出现在结果中
    //如果rdd2中有(a,4),(a,7),那匹配的结果会出现(a,(1,4)),(a,(1,7))
    val cogroupRDD = rdd1.cogroup(rdd2)
    cogroupRDD.collect().foreach(println)
    sc.stop()
  }
}
